#include <stdio.h>
#include <pthread.h>
#include <unistd.h>
#include <sys/syscall.h>

long retval = 10;

void* pthreadrun(void* arg)
{
  // long id = (long)arg;
  int count = 0;
  pthread_detach(pthread_self());
  while (1){
    printf("new threaad is running, pid is %d, thread id is %p\n", getpid(), pthread_self());
    sleep(1);
    if (++count == 3){
      pthread_exit(NULL);
    }
  }
}

int main()
{
  //pthread_t pthread[5];
  pthread_t thread;
  pthread_create(&thread, NULL, pthreadrun, NULL);
  sleep(1);
  if (pthread_join(thread, NULL) == 0){
    printf("wait success\n");
  }else{
    printf("wait failed\n");
  }

  //printf("main thread is waiting new thread\n");
  //void* ret = NULL;
  //pthread_join(thread, &ret);
  //printf("new thread has exited, exit code is %ld\n", (long)ret);
  
  //int i = 0;
  //for (; i < 5; ++i)
  //{
  //  // 创建新线程
  //  pthread_create(pthread+i, NULL, pthreadrun, (void*)i);
  //}
  //int count = 0;
  //while (1){
  //  printf("main thread is running, pid is %d, thread id is %p\n", getpid(), pthread_self());
  //  sleep(1);
  //  if (++count == 3){
  //    pthread_cancel(thread);
  //    printf("new thread is canceled...\n");
  //  }
  //}
  return 0;
}
